package com.example.lib5.linkedList.test;

/**
 * 创建日期：2022/6/10 0010 12:24
 *
 * @author tony.sun
 * 类说明：
 */
public class OutBoy {
    public static void main(String[] args) {
        CircleLinkedListC ls=new CircleLinkedListC();
        ls.addBoy(5);
        Boy count = ls.count(2, 5);
        System.out.println(count.toString());
    }
}
class CircleLinkedListC{

    Boy first=null;
    public void addBoy(int nums) {
        if (nums<=0){
            System.out.println("输入的人数不正确");
            return;
        }
        Boy cb=null;
        for (int i = 1; i < nums+1; i++) {Boy b=new Boy(i);
            if (first==null){
                first=b;
                b.setN(first);
                cb=first;
            }else {
                cb.setN(b);
                b.setN(first);
                cb=b;
            }
        }
    }

    public Boy count(int count, int nums) {
        if (count<=0||nums<=0||first==null){
            System.out.println("数据有误");
            return null;
        }
        Boy hel=first;
        while (hel.next!=first){
            hel=hel.getN();
        }
        while (true){
            if (first==hel){
                return hel;
            }
            for (int i = 0; i < count-1; i++) {
                first=first.getN();
                hel=hel.getN();
            }
            first=first.getN();
            hel.setN(first);
        }
    }
}
class Boy{
    int no;
    Boy next;
    public Boy(int no){
        this.no=no;
    }
    public Boy getN(){
        return next;
    }
    public void setN(Boy b){
        this.next=b;

    }

    @Override
    public String toString() {
        return "Boy{" +
                "no=" + no +
                '}';
    }
}